package cn.rokhdelar.ams.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysDepartment;
import cn.rokhdelar.ams.management.domain.query.DepartmentQuery;
import cn.rokhdelar.ams.management.domain.vo.DepartmentVO;
import cn.rokhdelar.ams.management.service.SysDepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Rokhdelar
 * @since 2024/10/12 -- 17:08
 **/
@RestController
@RequestMapping("/management/departments")
@Schema(title = "部门接口", description = "部门信息管理相关接口")
@RequiredArgsConstructor
public class DepartmentController {
    private final SysDepartmentService departmentService;

    @GetMapping
    @Operation(summary = "根据条件分页查询权限")
    public ResponseEntity<PageVO<DepartmentVO>> listDepartments(@ParameterObject DepartmentQuery query) {
        return ResponseEntity.ok(departmentService.getDepartmentListByPage(query));
    }

    @GetMapping(value = "/{id}" )
    @Operation(summary = "通过id查询部门")
    public ResponseEntity<DepartmentVO> getDepartmentById(@PathVariable("id") String id) {
        return ResponseEntity.ok(departmentService.getDepartmentById(id));
    }

    @PostMapping
    @Operation(summary = "新增部门")
    public ResponseEntity<Boolean> createDepartment(@RequestBody DepartmentVO department) {
        SysDepartment sysDepartment = BeanUtil.copyProperties(department, SysDepartment.class);
        return ResponseEntity.ok(departmentService.save(sysDepartment));
    }

    @PatchMapping(value = "/{id}" )
    @Operation(summary = "更新部门")
    public ResponseEntity<Boolean> updateDepartment(@RequestBody DepartmentVO department) {
        SysDepartment sysDepartment = BeanUtil.copyProperties(department, SysDepartment.class);
        return ResponseEntity.ok(departmentService.updateById(sysDepartment));
    }

    @DeleteMapping(value = "/{id}" )
    @Operation(summary = "通过id删除部门")
    public ResponseEntity<Boolean> deleteDepartment(@PathVariable("id") String id) {
        return ResponseEntity.ok(departmentService.removeById(id));
    }
}
